<template>
  <div class="chart-container" ref="chartRef"></div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref(null)
let chartInstance = null

onMounted(() => {
  nextTick(() => {
    initChart()
    window.addEventListener('resize', handleResize)
  })
})

const initChart = () => {
  chartInstance = echarts.init(chartRef.value)

  const option = {
    title: {
      text: '本月医生治疗患者排行',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      boundaryGap: [0, 0.01]
    },
    yAxis: {
      type: 'category',
      data: [
        '张医生',
        '李医生',
        '王医生',
        '赵医生',
        '陈医生'
      ]
    },
    series: [
      {
        name: '患者数量',
        type: 'bar',
        data: [135, 110, 90, 75, 60]
      }
    ]
  }

  chartInstance.setOption(option)
}

const handleResize = () => {
  chartInstance && chartInstance.resize()
}

// const onUnmounted = () => {
//   window.removeEventListener('resize', handleResize)
//   chartInstance && chartInstance.dispose()
// }
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 300px;
}
</style>
